/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * <dl>
 * <dd>描述: ~ CustomerSecurity</dd>
 * <dd>创建时间：  10:05 2021/3/6</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/3/6      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) //开启全局方法权限注解支持
public class CustomerSecurity extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomerUserDetail userDetail;

    /**
     * @Author: guodong
     * @Date: 10:07 2021/3/6
     * @ClassName: CustomerSecurity
     * @Parms [auth]
     * @ReturnType: void
     * @Description: 初始化用户角色信息
    */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        /*InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> configurer = auth.inMemoryAuthentication();
        configurer.withUser("admin").authorities("ROLE_ADMIN").password("{noop}1234");
        configurer.withUser("zhangsan").authorities("ROLE_01","add").password("{noop}1234");
        configurer.withUser("lisi").authorities("ROLE_02","find").password("{noop}1234");*/
        auth.userDetailsService(userDetail);
    }

    /**
     * @Author: guodong
     * @Date: 10:07 2021/3/6
     * @ClassName: CustomerSecurity
     * @Parms [http]
     * @ReturnType: void
     * @Description: 初始化资源信息
    */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin() //; //代表使用security默认的登录
                .loginProcessingUrl("/sec/login") //登录请求地址
                .loginPage("/login.html") //登录页面
                .defaultSuccessUrl("/main.html") //登录成功后默认页面
                .failureUrl("/login-fail.html"); //登录失败页面


        http.logout()
                .logoutUrl("/sec/logout") //登出请求地址
                .logoutSuccessUrl("/login.html"); //登出后的默认页面

        http.csrf().disable(); //跨站攻击

        http.exceptionHandling().accessDeniedPage("/auth-fail.html"); //权限不足页面

        http.authorizeRequests().antMatchers("*.js","*.css").permitAll(); //无需登录即可访问
        /*
            hasAnyAuthority: 拥有其中的某一项角色或者权限即可访问
            hasAuthority: 单个角色或者权限
            hasAnyRole: 拥有其中的某一项角色
            hasRole: 单个角色
            authenticated() 登录后即可访问
         */
        http.authorizeRequests().antMatchers("/pages/checkitem.html").hasAnyAuthority("ROLE_ADMIN", "add");
        http.authorizeRequests().antMatchers("/pages/checkgroup.html").hasAnyAuthority("find");
        http.authorizeRequests().antMatchers("/main.html","/pages/**").authenticated(); //登录后即可访问


    }
}
